package com.vanguard.codec;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

/**
 * @Title: 字符编码、解码工厂类，编码过滤工厂
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2018/12/25
 */
public class ClientCharsetCodecFactory implements ProtocolCodecFactory {

    private String messageType;

    private String charset;

    public ClientCharsetCodecFactory(String messageType, String charset) {
        this.messageType = messageType;
        this.charset = charset;
    }

    @Override
    public ProtocolEncoder getEncoder(IoSession ioSession) throws Exception {

        return new CharsetEncoder();
    }

    @Override
    public ProtocolDecoder getDecoder(IoSession ioSession) throws Exception {
        ProtocolDecoder protocolDecoder = null;
        switch (messageType) {
            case "FixedLength":
                protocolDecoder = new ClientFixedLengthDecode(charset);
                break;
            case "NotFixedLength":
            case "UserDefinedTailed":
                protocolDecoder = new ClientUserDefinedTailedDecode(charset);
                break;
            default:

        }
        return protocolDecoder;
    }
}
